package com.serkol.ams.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.entity.Goods;
import com.serkol.ams.entity.OrderDetail;
import com.serkol.ams.mapper.OrderDetailMapper;
import com.serkol.ams.modal.SqlFeildModal;
import com.serkol.ams.service.OrderDetailService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author pingan
 * @description 针对表【order_detail】的数据库操作Service实现
 * @createDate 2022-09-21 17:39:16
 */
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail>
        implements OrderDetailService {

    @Override
    public List<Map<String, Object>> findByOrderDetailList(long orderId) {

        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getOrderDetailFeild());
        queryWrapper.lambda().eq(OrderDetail::getOrderId, orderId);
        return this.listMaps(queryWrapper);
    }

    @Override
    public List<OrderDetail> findByOrderList(Long orderId) {
        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
    //    queryWrapper.select(SqlFeildModal.getOrderDetailFeild());
        queryWrapper.lambda().eq(OrderDetail::getOrderId, orderId);
        List<OrderDetail> maps = this.list(queryWrapper);
        return maps;
    }
    @Cacheable(value = "stastica", key = "'homeGoodsChartStastic::'+#type")
    @Override
    public List<Map<String, Object>> homeGoodsChartStastic(int type) {
        Date date = new Date();

        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getGoodsOrderDetailStatastica());
        queryWrapper.groupBy("name").orderByAsc("ymd");
        date = switch (type) {
            case 1 -> DateUtils.addMonths(date, -1);
            case 2 -> DateUtils.addMonths(date, -2);
            case 3 -> DateUtils.addMonths(date, -3);
            default -> date;
        };
    //    String format = new SimpleDateFormat("yyyy-MM").format(date);
        LocalDateTime startOfDay = DateUtil.toLocalDateTime(date);
        queryWrapper.lambda().ge(OrderDetail::getCreateDate, startOfDay);
        return this.listMaps(queryWrapper);

    }
}




